//
//  ViewController.m
//  UI-06-循环作业
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH    , HEIGHT)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.scrollView];
    [_scrollView release];
    
    self.scrollView.contentSize = CGSizeMake(9 * WIDTH, 0);
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageView.backgroundColor = [UIColor whiteColor];
    firstImageView.image = [UIImage imageNamed:@"h7.jpeg"];
    [self.scrollView addSubview:firstImageView];
    [firstImageView release];
    
    for (NSInteger i = 0; i < 8 ; i++) {
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg",i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        imageView.image = image;
        [self.scrollView addSubview:imageView];
        [imageView release];
    }
    
    UIImageView *lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(8 * WIDTH, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:lastImageView];
    [lastImageView release];
    
    self.scrollView.pagingEnabled = YES
    ;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollView.delegate = self;
    
    //    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    
    UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(00, 0, 130, 30)];
    page.center = CGPointMake(WIDTH / 2, HEIGHT - 30);
    page.numberOfPages = 5;
    page.backgroundColor = [UIColor clearColor];
    [self.view addSubview:page];
    [page addTarget:self action:@selector(page:) forControlEvents:(UIControlEventValueChanged)];
    [page release];
    
    
}

-(void)page:(UIPageControl *)page{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(WIDTH * (page.currentPage+1), 0);
    }];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.scrollView.contentOffset.x == 8 * WIDTH) {
        self.scrollView.contentOffset =CGPointMake(WIDTH, 0);
    }else if (self.scrollView.contentOffset.x == 0){
        self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
    }
}

-(void)changeImage{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0)animated:YES];
    if (self.scrollView.contentOffset.x
        == 5 * WIDTH) {
        self.scrollView.contentOffset =CGPointZero;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

